package com.module_live.view;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lib.app.CodeUtil;
import com.lib.fastkit.db.shared_prefrences.SharedPreferenceManager;
import com.lib.fastkit.http.ok.HttpUtils;
import com.lib.fastkit.utils.log.LogUtil;
import com.lib.fastkit.views.recyclerview.zhanghongyang.base.ViewHolder;
import com.lib.http.call_back.HttpNormalCallBack;
import com.lib.ui.adapter.BaseAdapter;
import com.lib.utls.glide.GlideConfig;
import com.module_live.R;
import com.module_live.activity.CoachStudyLivingActivity;
import com.module_live.bean.coachstudy.ApplyHandBean;
import com.module_live.bean.coachstudy.StudentRequestListBean;
import com.zyyoona7.popup.EasyPopup;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class StudentRequestListPopup {

    private Activity activity;

    RequestAdapter requestAdapter;

    List<StudentRequestListBean.ObjBean> requestList = new ArrayList<>();

    public StudentRequestListPopup(Activity activity) {

        this.activity = activity;

        initPopuPopu();
    }

    private EasyPopup popu;

    private void initPopuPopu() {
        popu = EasyPopup.create()
                .setContext(activity)
                .setContentView(R.layout.popup_study_requestlist)
                .setWidth(WindowManager.LayoutParams.FILL_PARENT)
                .setHeight(WindowManager.LayoutParams.FILL_PARENT)
                .setOnViewListener(new EasyPopup.OnViewListener() {
                    @Override
                    public void initViews(View view, EasyPopup basePopup) {

                        initView(view);

                        initRequestData();

                    }

                })
                .setFocusAndOutsideEnable(true)
                .apply();

    }

    private void initRequestData() {

        HttpUtils.with(activity)
                .addParam("requestType", "LIVE_RAISE_HANDS_LIST")
                .addParam("token", SharedPreferenceManager.getInstance(activity).getUserCache().getUserToken())
                .addParam("course_id", CoachStudyLivingActivity.course_id)
                .execute(new HttpNormalCallBack<StudentRequestListBean>() {
                    @Override
                    public void onSuccess(StudentRequestListBean result) {
                        if (CodeUtil.CODE_200 == result.getCode()) {

                            if (requestList.size() > 0) {
                                requestList.clear();
                            }
                            requestList.addAll(result.getObj());
                            requestAdapter.notifyDataSetChanged();
                        }

                    }

                    @Override
                    public void onError(String e) {

                    }
                });

    }

    public RecyclerView rvRequest;

    private void initView(View view) {
        rvRequest = view.findViewById(R.id.rv_request);

        RecyclerView rv_request = view.findViewById(R.id.rv_request);
        requestAdapter = new RequestAdapter(activity, requestList);
        rv_request.setLayoutManager(new LinearLayoutManager(activity));
        rv_request.setAdapter(requestAdapter);

    }

    public void updateRequestListData() {

        initRequestData();
    }

    public void dismiss() {
        popu.dismiss();
    }


    public void showAnswerPopuPopu(View view) {
        popu.showAtLocation(view, Gravity.CENTER, 0, 0);

    }


    private class RequestAdapter extends BaseAdapter<StudentRequestListBean.ObjBean> {

        public RequestAdapter(Context context, List<StudentRequestListBean.ObjBean> mData) {
            super(context, mData);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_request;
        }


        @Override
        protected void toBindViewHolder(ViewHolder holder, final int position, final List<StudentRequestListBean.ObjBean> mData) {

            final StudentRequestListBean.ObjBean objBean = mData.get(position);

            ImageView iv_head = holder.getView(R.id.iv_head);
            TextView tv_name = holder.getView(R.id.tv_name);
            TextView tv_action = holder.getView(R.id.tv_action);

            tv_name.setText(objBean.getUser_name());
            tv_action.setText("请求打开麦克风!");

            Glide.with(activity)
                    .load(objBean.getPhoto_url())
                    .apply(GlideConfig.getCircleOptions())
                    .into(iv_head);


            holder.getView(R.id.btn_yes).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    CoachStudyLivingActivity.studyControlFragment.requestBackOpenVoice(true, objBean.getPhone());
                    //发送取消举手请求至服务器
                    sendStudentApplyHandToService(objBean.getId());

                    requestList.remove(position);
                    notifyDataSetChanged();
                }
            });

            holder.getView(R.id.btn_no).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    CoachStudyLivingActivity.studyControlFragment.requestBackOpenVoice(false, objBean.getPhone());
                    //发送取消举手请求至服务器
                    sendStudentApplyHandToService(objBean.getId());

                    requestList.remove(position);
                    notifyDataSetChanged();
                }
            });

        }
    }


    public interface RequestVoicePopupListener {

        void onAllowVoice(String userPhone);

        void onRefuseVoice(String userPhone);
    }

    RequestVoicePopupListener listener;

    public void setRequestVoicePopupListener(RequestVoicePopupListener requestVoicePopupListener) {
        this.listener = requestVoicePopupListener;
    }


    public void sendStudentApplyHandToService(int studentId) {

        HttpUtils.with(activity)
                .addParam("requestType", "STUDENTS_RAISE_THEIR_HANDS")
                .addParam("token", SharedPreferenceManager.getInstance(activity).getUserCache().getUserToken())
                .addParam("course_id", CoachStudyLivingActivity.course_id)
                .addParam("student_id", studentId)
                .addParam("raise_hands", 0)
                .execute(new HttpNormalCallBack<ApplyHandBean>() {
                    @Override
                    public void onSuccess(ApplyHandBean result) {

                        if (CodeUtil.CODE_200 == result.getCode()) {
                            LogUtil.e("取消举手成功");

                        }
                    }

                    @Override
                    public void onError(String e) {

                    }
                });
    }

}
